﻿Imports NHibernate
Imports TodoMicroSolutionPackV1.dal.dao
Imports TodoMicroSolutionPackV1.entidades.dal.dto
Imports TodoMicroSolutionPackV1.facturacion.dal.dto
Public Class frmComprobanteSelect
    Private _comprobante As comprobante
    Private _comprobanteList As IList(Of comprobante)
    Private _cliente As entidad
    Private _session As ISession
    Private _DAOFactory As NhibernateDAOFactory

    Protected ReadOnly Property Session As ISession
        Get
            If _session Is Nothing Then
                _session = NhibernateSessionFactory.getSession
            End If
            Return _session
        End Get
    End Property

    Protected ReadOnly Property DAOFactory As NhibernateDAOFactory
        Get
            If _DAOFactory Is Nothing Then
                _DAOFactory = New NhibernateDAOFactory(Session)
            End If
            Return _DAOFactory
        End Get
    End Property

    Private Sub frmComprobanteSelect_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
    End Sub

    Private Sub frmComprobanteSelect_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Return Then
            btnBuscar.PerformClick()
        End If
    End Sub


    Private Sub frmComprobanteSelect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        comunes.populateCombobox(DAOFactory.getConstanteDAO.getConstantes("facturacion", "comprobante"), "valor", "codigo", cmbTipoComprobante)
        comunes.populateCombobox(DAOFactory.getPuntoVentaDAO.getPuntosDeVentaPorEmpresa(DAOFactory.getEmpresaDAO.FindByID(1)), "numero", "id", cmbPuntoVenta)
        Me.KeyPreview = True
    End Sub

    Private Sub btnSeleccionar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeleccionar.Click
        _cliente = comunes.getCliente(Session)
        If _cliente Is Nothing Then
            txtCliente.Text = ""
        Else
            txtCliente.Text = _cliente.razon_social
        End If
    End Sub
    Public Function getComprobante(ByRef session As ISession) As comprobante
        _session = session
        Me.ShowDialog()
        Return _comprobante
    End Function
    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        Dim numeroComprobante As Integer
        Try
            If txtNumeroComprobante.Text = "" Then
                numeroComprobante = 0
            Else
                numeroComprobante = CInt(txtNumeroComprobante.Text)
            End If
        Catch ex As Exception
            MsgBox("El numero de comprobante debe ser un valor entero")
            Return
        End Try
        _comprobanteList = DAOFactory.getComprobanteDAO.buscarComprobantes(cmbTipoComprobante.SelectedItem, _cliente, cmbPuntoVenta.SelectedItem, numeroComprobante)
        grdComprobantes.DataSource = _comprobanteList
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        _comprobante = _comprobanteList.Item(grdComprobantes.CurrentRow.Index)
        Me.Close()
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        _comprobante = Nothing
        Me.Close()
    End Sub
End Class